package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallActivityShareTeam;
import com.sc.nft.entity.MallOrderRefund;
import com.sc.nft.entity.dto.ActivityShareTeamPopUpDetailDTO;
import com.sc.nft.entity.dto.AdminMallActivityShareTeamPageDTO;
import com.sc.nft.entity.dto.MallActivityShareTeamDetailDTO;
import com.sc.nft.entity.dto.MallActivityShareTeamPageDTO;
import com.sc.nft.enums.MallRefundStatusEnum;
import com.sc.nft.enums.ShareTeamStatusEnum;
import com.sc.nft.mapper.MallActivityShareTeamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 分享购活动DAO
 *
 * @author liGen
 * @version 1.0
 * @date 2023/12/11 2:58 下午
 */
@Slf4j
@Repository
public class MallActivityShareTeamDao extends ServiceImpl<MallActivityShareTeamMapper, MallActivityShareTeam> {

    /**
     * 分享成团列表
     *
     * @param pageNo
     * @param pageSize
     * @param statusEnum 成团状态
     * @return
     */
    public Page<AdminMallActivityShareTeamPageDTO> getAdminMallActivityShareTeamPageDTO(Integer pageNo, Integer pageSize, ShareTeamStatusEnum statusEnum, Long activityShareId) {
        Integer status = null;
        if (Objects.nonNull(statusEnum)) {
            status = statusEnum.getCode();
        }
        return baseMapper.getAdminMallActivityShareTeamPageDTO(new Page(pageNo, pageSize), activityShareId, status);
    }

    /**
     * 用户分享成团列表
     *
     * @param pageNo
     * @param pageSize
     * @param userId   用户ID
     * @return
     */
    public Page<MallActivityShareTeamPageDTO> getMallActivityShareTeamPageDTO(Integer pageNo, Integer pageSize, Long userId) {
        return baseMapper.getMallActivityShareTeamPageDTO(new Page(pageNo, pageSize), userId);
    }

    /**
     * 用户分享成团详情
     *
     * @param userId
     * @param teamId
     * @return
     */
    public MallActivityShareTeamDetailDTO getMallActivityShareTeamDetailDTO(Long userId, Long teamId) {
        return baseMapper.getMallActivityShareTeamDetailDTO(userId, teamId);
    }

    /**
     * 获取分享团队弹窗信息
     *
     * @param teamId
     * @return
     */
    public ActivityShareTeamPopUpDetailDTO getActivityShareTeamPopUpDetailDTO(Long teamId) {
        return baseMapper.getActivityShareTeamPopUpDetailDTO(teamId);
    }

    /**
     * 是否已开团
     *
     * @param userId
     * @param activityShareId
     * @return
     */
    public MallActivityShareTeam hasShareTeamDetail(Long userId, Long activityShareId) {
        LambdaQueryWrapper<MallActivityShareTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivityShareTeam::getUserId, userId);
        wrapper.eq(MallActivityShareTeam::getActivityShareId, activityShareId);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 获取分享购的团队
     *
     * @param activityShareId
     * @param status
     * @return
     */
    public List<MallActivityShareTeam> getListByActivityShareId(Long activityShareId, ShareTeamStatusEnum status) {
        LambdaQueryWrapper<MallActivityShareTeam> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivityShareTeam::getStatus, status);
        wrapper.eq(MallActivityShareTeam::getActivityShareId, activityShareId);
        return baseMapper.selectList(wrapper);
    }

    /**
     * 修改成团状态
     *
     * @param id
     * @param status
     * @return
     */
    public boolean updateStatus(Long id, ShareTeamStatusEnum status) {
        UpdateWrapper<MallActivityShareTeam> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("status", status);
        if(ShareTeamStatusEnum.GROUP_SUCCESSFULLY == status){
            wrapper.set("is_issue", Boolean.TRUE);
        }
        return update(new MallActivityShareTeam(), wrapper);
    }

    /**
     * 修改成团是是否可以发放收益
     *
     * @param id
     * @param isCanIssue
     * @return
     */
    public boolean updateIsCanIssue(Long id, Boolean isCanIssue, Boolean updateEndTime) {
        UpdateWrapper<MallActivityShareTeam> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", id);
        wrapper.set("is_can_issue", isCanIssue);
        if (updateEndTime){
            wrapper.set("end_time", new Date());
        }
        return update(new MallActivityShareTeam(), wrapper);
    }

    /**
     * 添加成团订单数量
     *
     * @param id
     * @return
     */
    public boolean addOrderNumById(Long id, Integer count) {
        return baseMapper.addOrderNumById(id, count);
    }

    /**
     * 减少有效订单数量
     *
     * @param id
     * @return
     */
    public boolean subValidOrderNumById(Long id, Integer count) {
        return baseMapper.subValidOrderNumById(id, count);
    }

    /**
     * 获取上下架分享购的拼团中团队列表
     *
     * @return
     */
    public List<MallActivityShareTeam> getListByShareStatus() {
        return baseMapper.getList();
    }
}
